package com.le.dub.service.impl;

import com.le.dub.conf.RedisKey;
import com.le.dub.mobile.DubRecord;
import com.le.dub.service.IAppVoiceService;
import com.le.dub.service.IDubRecordService;
import com.le.dub.service.thread.CloneThread;
import com.le.dub.service.thread.VoiceThread;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
public class InitService {
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Resource
    private RedisTemplate template;
    @Resource
    private VoiceThread voiceThread;
    @Resource
    private CloneThread cloneThread;
    @Resource
    private IDubRecordService dubRecordService;
    @PostConstruct
    public void init(){
        //执行初始化代码
        System.out.println("执行初始化代码");
        Set<String> keys = template.keys("submit" + "*");
        if (keys != null && !keys.isEmpty()) {
            template.delete(keys);
        }
        template.opsForValue().set(RedisKey.executeVoiceNum,0);

        List<DubRecord> dubRecordList= dubRecordService.lambdaQuery().eq(DubRecord::getGenerateStatus,1).list();
        for (DubRecord dubRecord : dubRecordList) {
            template.opsForList().rightPush(RedisKey.voiceList, dubRecord.getId().toString());

        }
        threadPoolTaskExecutor.execute(voiceThread);
        threadPoolTaskExecutor.execute(cloneThread);

    }
}
